using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;

namespace Wicresoft.RDW.Utility
{
    public static class Helper
    {
        /// <summary>
        /// Set Panel child contols to editable
        /// </summary>
        /// <param name="panel">The parent container</param>
        /// <param name="isReadonly">true:set control to editable, false: set control to readonly</param>
        public static void ControlEditable(Control panel, bool editable)
        {
            if (panel == null || editable)
                return;
            Control[] ctls = panel.Controls.Cast<Control>().ToArray();
            for (int i = 0; i < ctls.Length; i++)
            {
                Control p = ctls[i];
                if (p is TextBox)
                {
                    TextBox q = p as TextBox;
                    q.Attributes.Add("disabled", "disabled");
                }
                else if (p is DropDownList)
                {
                    DropDownList q = p as DropDownList;
                    q.Attributes.Add("disabled", "disabled");
                }
                else if (p is CheckBox)
                {
                    CheckBox q = p as CheckBox;
                    q.Attributes.Add("disabled", "disabled");
                }
                else if (p is RadioButtonList)
                {
                    RadioButtonList q = p as RadioButtonList;
                    q.Attributes.Add("disabled", "disabled");
                }
                else if (p is CheckBoxList)
                {
                    CheckBoxList q = p as CheckBoxList;
                    q.Attributes.Add("disabled", "disabled");
                }
            }
        }
        public static void CopeProperties(this EntityObject fromObj, EntityObject toObj)
        {
            CopeProperties(fromObj, toObj, false);
        }
        public static void CopeProperties(this EntityObject fromObj, EntityObject toObj,bool withKey)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(fromObj.GetType());
            foreach (PropertyDescriptor p in properties)
            {
                object attr = p.Attributes[typeof(EdmScalarPropertyAttribute)];
                if (attr == null ||
                ((attr as EdmScalarPropertyAttribute).EntityKeyProperty) && !withKey)
                {
                    continue;
                }
                object obj = p.GetValue(fromObj);
                if (obj != null)
                {
                    p.SetValue(toObj, obj);
                }
            }
        }

        public static bool IsDebugMode()
        {
#if DEBUG
            return true;
#else
            return false;
#endif
        }

        public static bool IsReleaseMode()
        {
#if RELEASE
            return true;
#else
            return false;
#endif
        }

        public static bool IsAlcatelMode()
        {
#if ALCATEL 
            return true;
#else
            return false;
#endif
        }
        public static string FormateTimeSpan(TimeSpan span)
        {
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, span.Hours, span.Minutes, 0);
            return string.Format("{0:HH:mm}", time);
        }
    }

}
